package org.alfresco.replication.rules;

import java.util.ArrayList;
import java.util.List;

import org.alfresco.repo.model.Repository;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * Returns as target spaces the list of nodes provided as configuration.
 * 
 * @author Rui Fernandes
 * 
 */
public class SimpleReplicationTargetSelector implements
        ReplicationTargetSelector
{

	private List<String> nodePaths;
	private List<NodeRef> nodes;
	protected Repository repository;
	private static Log logger = LogFactory
	        .getLog(SimpleReplicationTargetSelector.class);

	public void setRepository(Repository repository)
	{
		this.repository = repository;
	}

	public void setNodePaths(List<String> nodePaths)
	{
		this.nodePaths = nodePaths;

	}

	private void initializeNodes()
	{
		nodes = new ArrayList<NodeRef>();
		for (String nodePath : nodePaths)
		{
			NodeRef node = repository.findNodeRef("path", nodePath.split("/"));
			logger.warn(String
			        .format("Added target space for automatic after completion replication rules triggering. PATH %s Node Id %s",
			                nodePath, node.getId()));
			nodes.add(node);
		}
		logger.warn(String
		        .format("%s target space(s) defined for automatic rules triggering after replication complete.",
		                nodes.size()));
	}

	@Override
	public List<NodeRef> getTargetSpaces(NodeRef reportNode)
	{
		if (nodes == null)
			initializeNodes();
		return nodes;
	}

}
